package com.xiaoai.timedtask.quartz;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.util.Date;

/**
 *
 * 任务启动
 * @Author
 * @Date 2021-08-30 23:01
 */
public class JobQuartz {
    public static void main(String[] args) throws SchedulerException {
        // scheduler工厂
        StdSchedulerFactory sf = new StdSchedulerFactory();

        // 调度器：scheduler
        Scheduler scheduler = sf.getScheduler();

        // job详情：jobDetail
        JobDataMap jdm = new JobDataMap();
        jdm.put("name1","xiaoai1");
        jdm.put("name2","xiaoai2");
        jdm.put("name3","xiaoai3");
        JobDetail jb = JobBuilder.newJob(MyJobbySync.class)
                .withDescription("this is a ram job") //job描述
                .withIdentity("ramJob","ramGroup") // job的name和 group
                .usingJobData("jobDetail","jobdetailPrams")
                .usingJobData("mergedAttr","mergedAttr-jobDetail")
                .usingJobData("name","xiaoai")  // 通过job属性name的set方法传递并赋值给属性
                .usingJobData(jdm)
                .build();

        // 触发器： trigger
        // --运行时间 3秒后启动
        long time = System.currentTimeMillis() + 3*1000l;
        Date startime = new Date(time);
        Trigger tg = TriggerBuilder.newTrigger()
                .withDescription("")
                .withIdentity("ramTrigger","ramTriggerGroup")
                .usingJobData("trigger","triggerPrams")
                .usingJobData("mergedAttr","mergedAttr-trigger")
//                .withSchedule(SimpleScheduleBuilder.simpleSchedule()) // 简单调度
                .withSchedule(CronScheduleBuilder.cronSchedule("0/5 * * * * ?"))  // 通过cron表达式调度
                .startNow()  // 立即执行
//                .startAt(startime)
                .build();

        // 注册绑定任务和定时器
        scheduler.scheduleJob(jb,tg);

        // 启动 调度器
        scheduler.start();

    }

}
